package com.stack.knowyouweb.controller;

import com.google.gson.Gson;

import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyoubean.bean.RepJson;
import com.stack.knowyouservice.service.EmailSendService;
import org.springframework.context.annotation.Scope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * 邮件验证码服务层
 *
 * @author stack
 */
@RestController
@Scope("singleton")
@RequestMapping("/api")
public class EmailController {
    @Resource
    private EmailSendService emailSendService;
    @Resource
    private Gson gson;
    @Resource
    private ThreadFactory threadFactory;

    @GetMapping("/captcha/register")
    public String sendRegisterCaptCha(@RequestParam(required = true, defaultValue = "") String mailTarget, HttpSession session) {
        String captCha = "";
        RepJson<String> repJson = new RepJson<>();
        try {
            captCha = emailSendService.sendRegisterCaptCha(mailTarget);
        } catch (Exception exception) {
            repJson.setCode(GlobalConst.ERROR_CODE);
            repJson.setMsg("验证码发送失败,系统繁忙");
        }
        if (StringUtils.isEmpty(captCha)) {
            repJson.setCode(GlobalConst.ERROR_CODE);
            repJson.setMsg("验证码发送失败,邮箱以被注册");
        } else {
            session.setAttribute("mailTarget", mailTarget);
            session.setAttribute("registerCaptCha", captCha);
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("验证码发送成功");
            //执行定时任务,5分钟验证码失效
            ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1, threadFactory);
            scheduledExecutorService.schedule(new Runnable() {
                @Override
                public void run() {
                    session.removeAttribute("registerCaptCha");
                }
            }, 5, TimeUnit.MINUTES);
        }
        return gson.toJson(repJson);
    }

    @GetMapping("/captcha/forgetPass")
    public String sendForgetPassCaptCha(@RequestParam(required = true, defaultValue = "") String username, HttpSession session) {
        String captCha = "";
        RepJson<String> repJson = new RepJson<>();
        try {
            captCha = emailSendService.sendForgetCaptCha(username);
        } catch (Exception exception) {
            repJson.setCode(GlobalConst.ERROR_CODE);
            repJson.setMsg("验证码发送失败,系统繁忙");
        }
        if (StringUtils.isEmpty(captCha)) {
            repJson.setCode(GlobalConst.ERROR_CODE);
            repJson.setMsg("验证码发送失败,账号/邮箱不存在");
        } else {
            session.setAttribute("username", username);
            session.setAttribute("forgetPassCaptCha", captCha);
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("验证码发送成功");
            ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1, threadFactory);
            scheduledExecutorService.schedule(new Runnable() {
                @Override
                public void run() {
                    session.removeAttribute("forgetPassCaptCha");
                }
            }, 5, TimeUnit.MINUTES);
        }
        return gson.toJson(repJson);
    }


}
